package cn.iocoder.yudao.module.team.controller.admin.linetrip;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.team.controller.admin.linetrip.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.linetrip.LineTripDO;
import cn.iocoder.yudao.module.team.service.linetrip.LineTripService;

@Tag(name = "管理后台 - 线路模板—行程")
@RestController
@RequestMapping("/team/line-trip")
@Validated
public class LineTripController {

    @Resource
    private LineTripService lineTripService;

    @PostMapping("/create")
    @Operation(summary = "创建线路模板—行程")
    @PreAuthorize("@ss.hasPermission('team:line-trip:create')")
    public CommonResult<Integer> createLineTrip(@Valid @RequestBody LineTripSaveReqVO createReqVO) {
        return success(lineTripService.createLineTrip(createReqVO));
    }
    @PutMapping("/createUpdateBatch")
    @Operation(summary = "更新线路模板—行程")
    @PreAuthorize("@ss.hasPermission('team:line-trip:update')")
    public CommonResult<Boolean> createUpdateBatch(@Valid @RequestBody List<LineTripDO> updateReqVO) {
        lineTripService.createUpdateBatchLineTrip(updateReqVO);
        return success(true);
    }
    @PutMapping("/update")
    @Operation(summary = "更新线路模板—行程")
    @PreAuthorize("@ss.hasPermission('team:line-trip:update')")
    public CommonResult<Boolean> updateLineTrip(@Valid @RequestBody LineTripSaveReqVO updateReqVO) {
        lineTripService.updateLineTrip(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除线路模板—行程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('team:line-trip:delete')")
    public CommonResult<Boolean> deleteLineTrip(@RequestParam("id") Integer id) {
        lineTripService.deleteLineTrip(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除线路模板—行程")
                @PreAuthorize("@ss.hasPermission('team:line-trip:delete')")
    public CommonResult<Boolean> deleteLineTripList(@RequestParam("ids") List<Integer> ids) {
        lineTripService.deleteLineTripListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得线路模板—行程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('team:line-trip:query')")
    public CommonResult<LineTripRespVO> getLineTrip(@RequestParam("id") Integer id) {
        LineTripDO lineTrip = lineTripService.getLineTrip(id);
        return success(BeanUtils.toBean(lineTrip, LineTripRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得线路模板—行程分页")
    @PreAuthorize("@ss.hasPermission('team:line-trip:query')")
    public CommonResult<PageResult<LineTripRespVO>> getLineTripPage(@Valid LineTripPageReqVO pageReqVO) {
        PageResult<LineTripDO> pageResult = lineTripService.getLineTripPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LineTripRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出线路模板—行程 Excel")
    @PreAuthorize("@ss.hasPermission('team:line-trip:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLineTripExcel(@Valid LineTripPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LineTripDO> list = lineTripService.getLineTripPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "线路模板—行程.xls", "数据", LineTripRespVO.class,
                        BeanUtils.toBean(list, LineTripRespVO.class));
    }

}